package utils;

import java.sql.*;

public class JDBCutils {
    private static Connection con;
    private static PreparedStatement ps;
    private static ResultSet rs;
    static {
        try{
            Class.forName("com.mysql.jdbc.Driver");
            String url="jdbc:mysql://localhost:3306/mysql";
            String user="root";
            String password="root";
            con = DriverManager.getConnection(url,user,password);
        }catch (Exception e){
            System.out.println(e+"数据库连接失败");
        }
    }
    public static int eu(String sql,Object[] object){
        int result=-1;
        try{
            ps=con.prepareStatement(sql);
            for(int i=0;i<object.length;i++){
                ps.setObject(i+1,object[i]);
            }
            result=ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    public static ResultSet eq(String sql){
        try{
            ps=con.prepareStatement(sql);
            rs=ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public static ResultSet eqo(String sql,String name){
        try{
            ps=con.prepareStatement(sql);
            ps.setObject(1,name);
            rs=ps.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rs;
    }
    public static void colse(){
        try{
            if(con!=null){
                con.close();
            }
            if(ps!=null){
                ps.close();
            }
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
